<template>
  <div class="container">
    <div style="width: 100%;padding: 20px">
      <div class="row" style="width: 100%">
        <Input v-model="input" placeholder="搜索题材" icon="ios-search" @on-click="doSearch" size="large"
               @keyup.enter="doSearch"/>
        <span style="height: 35px;font-size: 25px;width: 100%;text-align: center">{{currentNews.blockClassify}}</span>
      </div>

      <Table height="800" :columns="subjectHeader" :data="subjectData" border highlight-row
             @on-current-change="onSelected"
             style="margin-top: 20px;"></Table>
    </div>

    <Modal
      width="400"
      title="修改分类"
      @on-ok="commitSetNews"
      v-model="modalSetNews">
      <div class="col">
        <span style="margin-bottom: 10px">分类：</span>
        <Input v-model="currentNews.blockClassify" clearable disabled/>
        <span style="margin-top: 20px;margin-bottom: 10px">修正分类：</span>
        <div class="row between">
          <Select filterable style="width:200px" ref="select" clearable="true" v-model="newBlockClassify">
            <Option v-for="item in blockNameData" :value="item" :key="item">{{ item }}</Option>
          </Select>
          <Checkbox v-model="isPublic" >是否发布</Checkbox>
        </div>

      </div>
    </Modal>
  </div>
</template>

<script>

  import axios from './../assets/http/http'

  export default {
    name: 'NewsUncheck',
    data() {
      return {
        input: '',
        isPublic:false,
        modalSetNews: false,
        currentNews: '',
        blockNameData: [],
        newBlockClassify:'',
        subjectData: [],
        localData: [],
        subjectHeader: [
          {
            title: '新闻标题',
            key: 'title',
            minWidth: 100,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('div', {
                  domProps: {
                    innerHTML:params.row.title
                  },

                }, ""),
              ])
            }
          },
          {
            title: '概要',
            key: 'summary',
            minWidth: 400,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('div', {
                  domProps: {
                    innerHTML:params.row.summary
                  },

                }, ""),
              ])
            }
          },

          {
            title: '新闻时间',
            key: 'siteTime',
            minWidth: 70,
            align: 'center',

          },
          {
            title: '分类',
            key: 'blockClassify',
            minWidth: 30,
            align: 'center',

          },

          {
            title: '操作',
            key: '',
            minWidth: 50,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    type: 'success',
                    size: 'small'
                  },
                  style: {},
                  on: {
                    click: () => {
                      this.addNews(params.row)
                    }
                  }
                }, '入库'),
                h('Button', {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  style: {
                    marginLeft: '20px',
                  },
                  on: {
                    click: () => {
                      this.removeNews(params.row)
                    }
                  }
                }, '丢弃'),
              ]);
            }
          },

        ]
      }
    },

    methods: {
      onSelected(newItem, oldItem) {
        this.currentNews = newItem;
      },
      commitSetNews() {
        if (this.newBlockClassify === '' || this.newBlockClassify === undefined) {
          alert('请选择分类')
          return;
        }
        var isPublic = this.isPublic?'1':'0';
        axios.get('man/news/confirm?contentId=' + this.currentNews.contentId + '&blockClassify=' +
          this.currentNews.blockClassify + '&newBlockClassify=' + this.newBlockClassify.toString()+'&isPublish='+isPublic).
        then(rsp => {
          if (rsp.data.retCode === 1) {
            this.$Message.success('入库成功');
            this.currentNews.newBlockClassify = '';
            this.modalSetNews = false;
            this.initNewsList();
          }
        }).catch(error => {
          console.log(error);
        })
      },
      onSelectBlockName(value) {
        this.currentNews.newBlockClassify = value;
      },
      doSearch() {
//        if (this.input === null || this.input.trim() === "") {
//          this.subjectData = this.localData;
//        } else {
//          this.subjectData = [];
//          var result = this.input.trim().toLocaleLowerCase();
//          for (var i = 0; i < this.localData.length; i++) {
//            if (this.localData[i].blockClassify.indexOf(result) >= 0 || this.localData[i].summary.indexOf(result) >= 0
//              || this.localData[i].title.indexOf(result) >= 0) {
//              this.subjectData.push(this.localData[i]);
//            }
//          }
//        }

        axios.get(`search/news?keyword=${this.input}`).then(rsp => {
          if (rsp.data.retCode === 1) {
            if (rsp.data.retData) {
              this.localData = this.subjectData = [];
              this.localData = this.subjectData = rsp.data.retData;
            }
          }
        }).catch(error => {
          console.log(error);
        })

      },
      addNews(item) {
        this.currentNews = item;
        this.modalSetNews = true;
        this.newBlockClassify = this.currentNews.blockClassify;
        this.isPublic = false;
      },
      removeNews(item) {
        axios.get('man/news/confirm?contentId=' + this.currentNews.contentId + '&blockClassify=' +
          this.currentNews.blockClassify + '&newBlockClassify=未分类').then(rsp => {
          if (rsp.data.retCode === 1) {
            this.$Message.success('丢弃成功');
            this.modalSetNews = false;
            this.initNewsList();
          }
        }).catch(error => {
          console.log(error);
        })
      },
      initNewsList() {

      },

      initBlockNameData() {
        axios.get('news/listAllTopicName').then(rsp => {
          if (rsp.data.retCode === 1) {
            if (rsp.data.retData) {
              this.blockNameData = rsp.data.retData;

            }

          }
        }).catch(error => {
          console.log(error);
        })
      },
    },
    created() {
      this.initNewsList();
      this.initBlockNameData();
    }
  }
</script>

<style scoped>
  .container {
    height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px 0;
    box-sizing: border-box;
  }

  @media screen and (min-width: 1281px) and (max-width: 2000px) {

  }


</style>
